# -*- coding: utf-8 -*-
# @Time : 2025/2/27 15:59
# @Author : debbyszhang
# @File : user_list_page.py
# @desc :
import time

from playwright.sync_api import Page

from libs.base_page import BasePage


class UserListPage(BasePage):
    def __init__(self, page: Page):
        super().__init__(page)
        # self.url = "http://192.168.0.173:8088"
        self.page.get_by_role("link", name="用户列表").click()

    def new_user(self, username: str, password):
        self.page.get_by_role("button", name="新建用户").click()
        username_input = self.page.locator("input[name=\"userName\"]")
        username_input.fill(username)
        self.click_fill("请输入密码", password)
        self.page.get_by_text("请选择用户角色").click()
        self.page.get_by_role("listitem", name="跨链接入方").click()
        self.click("确定")
        t = self.page.locator("#tea-message-root div").inner_text()
        print(t)  # '创建用户成功'

    def get_user_list(self):
        # 获取表头
        headers = self.page.locator("table th").all()
        headers = [header.text_content() for header in headers]

        # 获取所有表格行
        rows = self.page.locator("table tbody tr").all()

        data_list = []
        for row in rows:
            # 获取当前行的所有单元格
            cells = row.locator("td").all()
            cells = [cell.text_content() for cell in cells]

            # 创建字典并添加到列表中
            data_dict = dict(zip(headers, cells))
            data_list.append(data_dict)

        return data_list

    def search_user(self, content):
        self.click_fill("请输入用户名", content)
        self.click("搜索")

    def disable_user(self, row_number=None, username=None):
        # 定位操作按钮
        if row_number:
            # 根据行号定位
            rows = self.page.locator("table tbody tr")
            if rows.count() == 0:
                print("列表中没有数据")
            first_row = rows.nth(row_number)
            operation = (first_row.locator('td >> nth=7'))
            if '禁用' in operation.text_content():
                operation.locator('button', has_text='禁用').click()
            elif '启用' in   operation.text_content():
                operation.locator('button', has_text='启用').click()
            # self.click('确认')

        elif username:
            self.page.get_by_role("row", name=username).get_by_role("button").first.click()
        self.click('确认')

    def edit_user(self, row_number=None, username=None):
        if row_number:
            # 根据行号定位
            rows = self.page.locator("table tbody tr")
            if rows.count() == 0:
                print("列表中没有数据")
            first_row = rows.nth(row_number)
            operation = (first_row.locator('td >> nth=7'))
            operation.locator('button', has_text='编辑').click()
        elif username:
            self.page.get_by_role("row", name=username).get_by_role("button").nth(1).click()
        self.click('确定')

    def reset_pwd(self, row_number=None, username=None):
        if row_number:
            # 根据行号定位
            rows = self.page.locator("table tbody tr")
            if rows.count() == 0:
                print("列表中没有数据")
            first_row = rows.nth(row_number)
            operation = (first_row.locator('td >> nth=7'))
            operation.locator('button', has_text='重置密码').click()
            self.click_fill("请输入新密码", 'abcd1234')

        elif username:
            self.page.get_by_role("row", name=username).get_by_role("button").nth(2).click()
            self.click_fill("请输入新密码", 'abcd1234')
        self.click('确定')
